package com.itheima.controller;

import com.itheima.anno.Flag;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 班级管理
 */
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 查询所有班级
     * @return
     */
    @GetMapping("list")
    public Result list() {
        List<Clazz> list = clazzService.list();
        return Result.success(list);
    }

    /**
     * 添加班级
     * @param clazz
     * @return
     */
    @PostMapping
    @Flag
    public Result add(@RequestBody Clazz clazz) {
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 分页查询班级
     * @param name
     * @param begin
     * @param end
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result queryClazz(String name,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
         PageResult pageResult =clazzService.page(name, begin, end,page, pageSize);
         return Result.success(pageResult);
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Flag
    public Result delete(@PathVariable Integer id) {
         clazzService.delete(id);
         return Result.success();
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级
     * @param clazz
     * @return
     */
    @PutMapping
    @Flag
    public Result update(@RequestBody Clazz clazz) {
         clazzService.update(clazz);
         return Result.success();
    }
}
